package com.example.service.impl;

import com.example.dao.MemberDao;
import com.example.dao.MessageDao;
import com.example.entity.Member;
import com.example.entity.Message;
import com.example.service.MessageService;
import com.example.util.Info;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 留言服务实现类
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDao messageDao;
    
    @Resource
    private MemberDao memberDao;

    @Override
    public PageInfo<Message> getMessageList(Integer pageNum, Integer pageSize, String key, String memberid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("memberid", memberid);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Message> list = messageDao.selectAll(map);
        
        // 填充会员信息
        for (Message message : list) {
            Member member = memberDao.findById(message.getMemberid());
            message.setUsername(member.getUsername() + " / " + member.getName());
            message.setMember(member);
        }
        
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Message message) {
        if (message.getId() == null) {
            messageDao.add(message);
        } else {
            message.setHftime(Info.getDateStr());
            messageDao.update(message);
        }
    }

    @Override
    public void deleteMessage(int id) {
        messageDao.delete(id);
    }

    @Override
    public void batchDeleteMessage(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            messageDao.delete(Integer.parseInt(id));
        }
    }
} 